package com.whmall.service.crm.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.whmall.service.crm.entity.dto.priceconfig.SupplierPriceRoundConfiguration;
import com.whmall.service.crm.module.suppliercreditscore.SupplierCreditScoreSetting;
import com.whmall.service.crm.module.supplierreview.entity.SupplierReviewTagCategoryVO;
import com.whmall.service.crm.module.supplierreview.entity.SupplierReviewTagVO;
import com.whmall.service.crm.module.supplierreview.entity.SupplierUserReviewTagVO;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Redis配置
 */
@Configuration
public class RedisConfiguraion {

    private final ObjectMapper objectMapper;

    public RedisConfiguraion(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Bean
    public RedisTemplate<String, Long> longRedisTemplate(
            RedisConnectionFactory redisConnectionFactory) {
        return buildJackson2ClassRedisTemplate(redisConnectionFactory, Long.class);
    }

    @Bean
    public RedisTemplate<String, String> stringRedisTemplate(
            RedisConnectionFactory redisConnectionFactory) {
        return buildJackson2ClassRedisTemplate(redisConnectionFactory, String.class);
    }

    @Bean
    public RedisTemplate<String, SupplierCreditScoreSetting> supplierCreditScoreSettingRedisTemplate(
            RedisConnectionFactory redisConnectionFactory) {
        return buildJackson2ClassRedisTemplate(redisConnectionFactory, SupplierCreditScoreSetting.class);
    }

    @Bean
    public RedisTemplate<String, SupplierReviewTagCategoryVO> supplierReviewTagCategoryRedisTemplate(
            RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, SupplierReviewTagCategoryVO> template
                = buildJackson2ClassRedisTemplate(redisConnectionFactory, SupplierReviewTagCategoryVO.class);
        template.setHashKeySerializer(new Jackson2JsonRedisSerializer<>(Integer.class));
        return template;
    }

    @Bean
    public RedisTemplate<String, SupplierReviewTagVO> supplierReviewTagRedisTemplate(
            RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, SupplierReviewTagVO> template
                = buildJackson2ClassRedisTemplate(redisConnectionFactory, SupplierReviewTagVO.class);
        template.setHashKeySerializer(new Jackson2JsonRedisSerializer<>(Integer.class));
        return template;
    }

    @Bean
    public RedisTemplate<String, SupplierUserReviewTagVO> supplierUserReviewTagRedisTemplate(
            RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, SupplierUserReviewTagVO> template
                = buildJackson2ClassRedisTemplate(redisConnectionFactory, SupplierUserReviewTagVO.class);
        template.setHashKeySerializer(new Jackson2JsonRedisSerializer<>(Integer.class));
        return template;
    }

    @Bean
    public RedisTemplate<String, SupplierPriceRoundConfiguration> supplierPriceRoundConfigurationRedisTemplate(
            RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, SupplierPriceRoundConfiguration> template
                = buildJackson2ClassRedisTemplate(redisConnectionFactory, SupplierPriceRoundConfiguration.class);
        template.setHashKeySerializer(new Jackson2JsonRedisSerializer<>(Integer.class));
        return template;
    }

    private <T> RedisTemplate<String, T> buildJackson2ClassRedisTemplate(
            RedisConnectionFactory redisConnectionFactory, Class<T> clazz) {
        RedisTemplate<String, T> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer<T> serializer = new Jackson2JsonRedisSerializer<>(clazz);
        serializer.setObjectMapper(objectMapper);
        template.setKeySerializer(stringRedisSerializer);
        template.setValueSerializer(serializer);
        template.setHashKeySerializer(stringRedisSerializer);
        template.setHashValueSerializer(serializer);
        return template;
    }

    @Bean(destroyMethod = "shutdown")
    public RedissonClient redissonClient(RedisProperties redisProperties) {
        Config config = new Config();
        config.useSingleServer()
                .setAddress(String.format("redis://%s:%s", redisProperties.getHost(), redisProperties.getPort()))
                .setDatabase(redisProperties.getDatabase())
                .setConnectionPoolSize(8)
                .setConnectionMinimumIdleSize(8)
        ;
        if (StringUtils.isNotBlank(redisProperties.getPassword())) {
            config.useSingleServer().setPassword(redisProperties.getPassword());
        }
        return Redisson.create(config);
    }
}
